<?php

namespace app\checkbill\logic;
use think\facade\Db;
class PayCheckLogic
{



    /**
     * @desc   tik面板回款流水
     * @author luckyXiong
     * @date   2024-05-31
     */
    public function pay_list($params=[]){
        $shop_id   = $params['shop_id'] ?? '';
        $date      = $params['date'] ?? '';
        $paid_time = $params['paid_time'] ?? '';
        $currency  = $params['status'] ?? 'USD';
        $limit     = $params['limit'] ?? 10;

        $where = [];
        $order = [];
        if ($shop_id) {
            $where[] = ['shop_id', '=', $shop_id];
        }

        if ($date) {
            $where[] = ['date', '=', $date];
        }

        if ($paid_time) {
            $createtimeArr = explode(',', $paid_time);
            $where[] = ['paid_time', '>=', strtotime($createtimeArr[0])];
            $where[] = ['paid_time', '<=', strtotime($createtimeArr[1])];
        }

        if ($currency) {
            $where[] = ['currency', '=', $currency];
        }

        $where[] = ['pay_status', '=', 'PAID'];

        $field = 'id,date,shop_id,bank_account,settlement_amount,
                  reserve_amount,amount,pay_status,currency,createtime,payment_amount_before_exchange,paid_time';

        $res = Db::name('bank_pay_info')
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $list = $res->items()?:[];
        $list = objectToArray($list);
        $pay_status = Config('Logisticskey.pay_status');

        //批量获取店铺信息及sku数量信息;
        $shop_ids      = array_column($list,'shop_id');
        $shopInfos     = Db::name('shops')->whereIn('id',$shop_ids)->field('name,id')->select()->toArray();
        $shopInfos_map = array_column($shopInfos,null,'id');
        foreach($list as $k=>$v){
            $shop_name               = $shopInfos_map[$v['shop_id']]??[];
            $list[$k]['shop_name']   = $shop_name['name']??'';
            $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['createtime']);
            $list[$k]['paid_time']   = date("Y-m-d H:i:s", $v['paid_time']);
            $list[$k]['pay_status']  = $pay_status[$v['pay_status']];
            unset($list[$k]['createtime']);
        }

     
        $total_info   = Db::name('bank_pay_info')->where($where)->field('id,amount')->select()->toArray();
        $total_amount = array_sum(array_column($total_info, 'amount'));
        $total_amount = getRes($total_amount);

        return [
            'list'          => $list,
            'total'         => $res->total(),
            'total_amount'  => $total_amount,
        ];
    }



    /**
     * @desc   云汇回款流水
     * @author luckyXiong
     * @date   2024-06-04
     */
    public function yunhui_list($params=[]){

        $shop_id     = $params['shop_id'] ?? '';
        $date        = $params['date'] ?? '';
        $limit       = $params['limit'] ?? 10;
        $createtime  = $params['create_time'] ?? '';

        $where = [];
        $order = [];
        if ($shop_id) {
            $where[] = ['shop_id', '=', $shop_id];
        }

        if ($date) {
            $where[] = ['date', '=', $date];
        }


        if ($createtime) {
            $createtimeArr = explode(',', $createtime);
            $where[] = ['created_at', '>=', strtotime($createtimeArr[0])];
            $where[] = ['created_at', '<=', strtotime($createtimeArr[1])];
        }

        $where[] = ['pay_id', '<>', 0];

        $field = 'id,date,shop_id,amount,status,payer_name,currency,created_at as createtime';

        $res = Db::name('bank_pay_yunhui')
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $list = $res->items()?:[];
        $list = objectToArray($list);
        $pay_status = Config('Logisticskey.yunhui_status');

        //批量获取店铺信息及sku数量信息;
        $shop_ids      = array_column($list,'shop_id');
        $shopInfos     = Db::name('shops')->whereIn('id',$shop_ids)->field('name,id')->select()->toArray();
        $shopInfos_map = array_column($shopInfos,null,'id');
        foreach($list as $k=>$v){
            $shop_name               = $shopInfos_map[$v['shop_id']]??[];
            $list[$k]['shop_name']   = $shop_name['name']??'';
            $list[$k]['create_time'] = date("Y-m-d H:i:s",$v['createtime']);
            $list[$k]['payer_name']  = 'TikTok Shop';
            $list[$k]['pay_status']  = $pay_status[$v['status']];
            unset($list[$k]['createtime']);
            unset($list[$k]['status']);
        }

        $total_info   = Db::name('bank_pay_yunhui')->where($where)->field('id,amount')->select()->toArray();
        $total_amount = array_sum(array_column($total_info, 'amount'));

        $total_amount = getRes($total_amount);

        return [
            'list'          => $list,
            'total'         => $res->total(),
            'total_amount'  => $total_amount
        ];










    }





}